<?php

/**
 * image actions.
 *
 * @package    blogallery
 * @subpackage image
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class imageActions extends sfActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex()
  {
    $c = new Criteria();
    
    // sorted by date by default
    $c->addAscendingOrderByColumn(ImagePeer::DATETIME);
    
 	  $this->pager = $this->getPager($c);
  }
  
  public function executeShow()
  {
    $this->image = ImagePeer::retrieveByPkWithI18n($this->getRequestParameter('id'));
    $this->forward404Unless($this->image instanceof Image);
    $this->forward404Unless($this->image->getPost()->getPublished());
  }
  
  public function executeFull()
  {
    $this->image = ImagePeer::retrieveByPkWithI18n($this->getRequestParameter('id'));
    if (!$this->image instanceof Image)
    {
      return sfView::NONE;
    }
    $this->setTemplate('show');
  }
    
  
  public function executeTag()
  {
    $c = new Criteria();
    
    $crit = new Criteria();
    $crit->add(TagPeer::HANDLE, $this->getRequestParameter('tag'));
    $crit->addJoin(PostTagPeer::TAG_ID, TagPeer::ID, Criteria::LEFT_JOIN);
    foreach (PostTagPeer::doSelectJoinPost($crit) as $post_tag)
    {
      if ($post_tag->getPost()->getPublished())
      {
        $c->addOr($c->getNewCriterion(ImagePeer::POST_ID, $post_tag->getPostId()));
      }
    }
    
    // sorted by date by default
    $c->addAscendingOrderByColumn(ImagePeer::DATETIME);

 	  $this->pager = $this->getPager($c);
    
    $this->setTemplate('index');
  }
  
  private function getPager($c)
  {
    $c->addJoin(ImagePeer::ID, ImageI18nPeer::ID);
    $c->add(ImageI18nPeer::CULTURE, $this->getUser()->getCulture());
 	  $pager = new sfPropelPager('Image', sfConfig::get('app_max_image'));
 	  $pager->setCriteria($c);
    $pager->setPeerMethod('doSelectWithI18n');
    $pager->setPage($this->getRequestParameter('page', 1));
 	  $pager->init();
 	  return $pager;
  }
  
  public function executeCreate()
  {
    $this->image = new Image();
    $this->number = (integer)$this->getRequestParameter('number');
    $this->languages = LanguagePeer::doSelect(new Criteria());
  }
  
  public function executeCancel()
  {
    return sfView::NONE;
  }
}
